package di.ass.view;

import java.util.ArrayList;

import di.ass.database.DatabaseHandler;
import di.ass.database.Formula;
import di.ass.edit.AlertsEdit;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnDismissListener;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import di.ass.R;

public class ListFormulaAdapter extends ArrayAdapter<Formula> {

	private Context context;
	LinearLayout linearLayout;
	CheckBox check_formula;
	TextView formalu_content;
	TextView items;
	TextView date_formula;
	ImageView image;
	DatabaseHandler db;
	private ArrayList<Formula> arrayFormula;

	public ListFormulaAdapter(Context context, int textViewResourceId,
			ArrayList<Formula> objects) {
		super(context, textViewResourceId, objects);
		// TODO Auto-generated constructor stub
		this.context = context;
		arrayFormula = objects;
		db = new DatabaseHandler(context);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		View vi = convertView;

		if (vi == null) {
			vi = new ElementListFormula(context);
		}

		final Formula data = arrayFormula.get(position);
		if (data != null) {

			check_formula = ((ElementListFormula) vi).getCheck_formula();
			formalu_content = ((ElementListFormula) vi).getFormula_content();
			date_formula = ((ElementListFormula) vi).getDate_formula();
			image = ((ElementListFormula) vi).getImageCheck();
			items = ((ElementListFormula) vi).getStatus();
			linearLayout = ((ElementListFormula) vi).getLinearLayout();
			final Bundle myBundle = new Bundle();
			// Kich bieu tuong xem
			linearLayout.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					Intent intentView = new Intent("di.ass.view.LISTFOOD");
					myBundle.putInt("idFormula", data.getId());
					myBundle.putString("dateFormula",
							db.getFormula(data.getId()).getDate().toString());
					myBundle.putString("nameFormula",
							db.getFormula(data.getId()).getName().toString());
					intentView.putExtras(myBundle);
					context.startActivity(intentView);

				}
			});
			// Sua thong tin thuc don
			linearLayout.setOnLongClickListener(new OnLongClickListener() {
				@Override
				public boolean onLongClick(View v) {
					// TODO Auto-generated method stub
					AlertDialog alertDialog = AlertsEdit.ShowEditFormulaDialog(
							context, db.getFormula(data.getId()));
					alertDialog.setOnDismissListener(new OnDismissListener() {
						@Override
						public void onDismiss(DialogInterface dialog) {
							// TODO Auto-generated method stub
							load(db.getFormula(data.getId()));
							notifyDataSetChanged();
						}
					});
					alertDialog.show();
					return true;
				}
			});
			if (data.getFull() == 1)
				image.setImageResource(R.drawable.formula_full);
			else if (data.getFull() == 0)
				image.setImageResource(R.drawable.formula_not_full);
			else
				image.setImageResource(R.drawable.formula_empty);

			check_formula
					.setOnCheckedChangeListener(new OnCheckedChangeListener() {

						@Override
						public void onCheckedChanged(CompoundButton arg0,
								boolean arg1) {
							// TODO Auto-generated method stub
							data.setChecked(arg1);
						}
					});
			int countAllFood = db.getFoodsCount(data.getId(), 1)
					+ db.getFoodsCount(data.getId(), 0);
			int countDoneFood = db.getFoodsCount(data.getId(), 1);
			String status = null;
			if (db.getLanguageUse().equals("Việt Nam"))
				status = "Đã xong: " + countDoneFood + "/" + countAllFood;
			else if (db.getLanguageUse().equals("English")) {
				status = "Done: " + countDoneFood + "/" + countAllFood;
			}
			items.setText(status);
			load(db.getFormula(data.getId()));
		}
		notifyDataSetChanged();
		return vi;
	}

	public void load(Formula formula) {
		String date = formula.getDate();
		String[] temp;
		temp = date.split("/");
		String strDate = null;
		if (db.getLanguageUse().equals("Việt Nam"))
			strDate = temp[2] + "/" + temp[1] + "/" + temp[0];
		else if (db.getLanguageUse().equals("English")) {
			strDate = temp[1] + "/" + temp[2] + "/" + temp[0];
		}
		formalu_content.setText(formula.getName());
		date_formula.setText(strDate);
		check_formula.setChecked(formula.getChecked());
	}

}
